from PIL import Image
import os

def resize_and_replace_images(source_path):
    # 获取源路径下的所有文件和文件夹
    for root, dirs, files in os.walk(source_path):
        for file in files:
            # 检查文件是否为图片
            if file.endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp')):
                # 构建源文件的完整路径
                source_file_path = os.path.join(root, file)
                # 打开图片
                image = Image.open(source_file_path)
                # 调整尺寸为 256x256
                resized_image = image.resize((256, 256))
                # 保存调整尺寸后的图片
                os.remove(source_file_path)
                resized_image.save(source_file_path)
                # 删除原始图片
                print(f"Resized and replaced {file}")

# 源路径
source_path = '/media/jolly/HIK/Brain Tumor Segmentation Dataset/image/predictResult/'

# 调用函数处理图片
resize_and_replace_images(source_path)
